//
//  LoginViewController.m
//  CivilianTravel
//
//  Created by 许仕杰 on 16/7/14.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "FMDBData.h"
#import "Account.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UMSocial.h"
#import "PassValue.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancel;
@property (weak, nonatomic) IBOutlet UIButton *regis;

@property (weak, nonatomic) IBOutlet UIView *starcview;
@property(nonatomic,strong)MPMoviePlayerController *player;
@end

@implementation LoginViewController
-(MPMoviePlayerController *)player{
    if (!_player) {
        _player =[[MPMoviePlayerController alloc]init];
        _player.view.frame = [UIScreen mainScreen].bounds;
        _player.controlStyle = MPMovieControlStyleNone;
        [self.view addSubview:self.player.view];
        
        
    }return _player;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    FMDBData *dbdata=  [FMDBData sharedFMDB];
    Account *accoun = [dbdata select];
    self.name.text = accoun.uname;
    self.passWord.text = accoun.password;
    self.passWord.secureTextEntry=YES;
    
    NSString *string = [[NSBundle mainBundle]pathForResource:@"keep" ofType:@"mp4"];
    self.player.contentURL = [[NSURL alloc]initFileURLWithPath:string];
    [self.player play];
    [self.player setRepeatMode:MPMovieRepeatModeOne];
    [self.view bringSubviewToFront:self.name];
    [self.view bringSubviewToFront:self.passWord];
    [self.view bringSubviewToFront:self.cancel];
    [self.view bringSubviewToFront:self.regis];
    [self.view bringSubviewToFront:self.loginBtn];
    [self.view bringSubviewToFront:self.starcview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
//注册
- (IBAction)registerAction:(id)sender {
    RegisterViewController *registe = [[RegisterViewController alloc] init];
    
    
    __weak typeof(self) weakself = self;//或者是这样写
    registe.block = ^(NSString *astring,NSString *astring2){
        weakself.name.text =astring;
        weakself.passWord.text = astring2;
        
    };
    
    [self presentViewController:registe animated:YES completion:nil];
}
//取消
- (IBAction)cancleAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//登录
- (IBAction)loginAction:(id)sender {
    EMError *error = [[EMClient sharedClient] loginWithUsername:self.name.text password:self.passWord.text];
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!error) {
            NSLog(@"登录成功");
            
            //建表
            FMDBData *fmdbdate =   [FMDBData sharedFMDB];
            [fmdbdate creatTableSqlString:@"create table if not exists account (uname text,password text,gender text,age text)"];
            
        
            [fmdbdate insertDataWithSqlString:self.name.text string:self.passWord.text];
            
            self.block(self.name.text);
            
            
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            NSLog(@"%@",error);
        }
        
        
    });
    
}
//qq
- (IBAction)qqAction:(id)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //   获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            [PassValue sharePassValue].username = snsAccount.userName;
            [PassValue sharePassValue].iconUrl = snsAccount.iconURL;

            [PassValue sharePassValue].gender = response.thirdPlatformUserProfile[@"gender"];
            [PassValue sharePassValue].city = response.thirdPlatformUserProfile[@"city"];
            [self dismissViewControllerAnimated:YES completion:nil];

        }});
}
//微信
- (IBAction)weixinAction:(id)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //   获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            [PassValue sharePassValue].username = snsAccount.userName;
            [PassValue sharePassValue].iconUrl = snsAccount.iconURL;
            [PassValue sharePassValue].gender = response.thirdPlatformUserProfile[@"gender"];
            [PassValue sharePassValue].city = response.thirdPlatformUserProfile[@"city"];

            [self dismissViewControllerAnimated:YES completion:nil];

        }});
}
//微博
- (IBAction)weiboAction:(id)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //   获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            
            
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            [PassValue sharePassValue].username = snsAccount.userName;
            [PassValue sharePassValue].iconUrl = snsAccount.iconURL;
//            pass.gender = response.thirdPlatformUserProfile[@"gender"];
//            pass.city = response.thirdPlatformUserProfile[@"city"];

            [self dismissViewControllerAnimated:YES completion:nil];

        }});

}


@end
